<?php

namespace App\Admin\Forms;

use App\Models\AdminConfig as AdminConfigModel;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Symfony\Component\HttpFoundation\Response;

class AdminConfig extends Form {
    protected $group_id = 0;

    public function index(Content $content) {
        $content->withError('Title', 'messages..');
        die();
        return $content;
    }
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return Response
     */
    public function handle(array $input) {
        foreach ($input as $k => $v) {
            AdminConfigModel::where("name", $k)->limit(1)->update(['value' => $v]);
            AdminConfigModel::loadConfig($k);
        }
        $fields = AdminConfigModel::orderBy("sort", "asc")
            ->orderBy("id", "asc")->get();
        $datas = [];
        return $this->response()->success('保存成功.', '/settings');
    }

    /**
     * Build a form here.
     */
    public function form() {

        $groups = AdminConfigModel::getConfigGroup();
        foreach ($groups as $group_id => $group) {

            $this->tab($group, function (Form $form) use($group_id) {
                $fields = AdminConfigModel::where("group", $group_id)
                    ->orderBy("sort", "asc")->orderBy("id", "asc")->get();

                foreach ($fields as $field) {
                    $options = [];
                    $arr = explode(",", str_ireplace("\r\n", ",", $field->options));
                    if (gettype($arr) == 'string') {
                        $arr = [$arr];
                    }

                    foreach ($arr as $key => $val) {
                        $arr2 = explode(":", $val);
                        if (gettype($arr2) == "array" && count($arr2) > 1) {
                            $options[$arr2[0]] = $arr2[1];
                        }
                    }
                    $holder = empty($field->placeholder) ? "请输入" . $field->title : $field->placeholder;
                    switch ($field->type) {
                        case 'select':
                            $form->select($field->name, $field->title)
                                ->placeholder($holder)
                                ->options($options)->value($field->value);
                            break;
                        case 'radio':
                            $form->radio($field->name, $field->title)
                                ->placeholder($holder)
                                ->help($field->help)
                                ->options($options)->value($field->value);
                            break;
                        case 'checkbox':
                            $form->checkbox($field->name, $field->title)
                                ->placeholder($holder)
                                ->help($field->help)
                                ->options($options)->value($field->value);
                            break;

                        case 'date':
                            $form->date($field->name, $field->title)
                                ->placeholder($holder)
                                ->help($field->help)
                                ->value($field->value);
                            break;
                        case 'datetime':
                            $form->datetime($field->name, $field->title)
                                ->placeholder($holder)
                                ->value($field->value);
                            break;
                        case 'image':
                            $form->image($field->name, $field->title)
                                ->accept('jpg,png,gif,jpeg,bmp,jpg', 'image/*')
                                ->placeholder($holder)
                                ->url("upload")
                                ->default($field->value);
                            break;
                        case 'input':
                        case 'number':
                            if (!empty($field->help) && trim($field->help) != '') {
                                $form->text($field->name, $field->title)
                                    ->help($field->help)
                                    ->placeholder($holder)
                                    ->value($field->value);
                            } else {
                                $form->text($field->name, $field->title)
                                    ->placeholder($holder)
                                    ->value($field->value);
                            }
                            break;

                        case 'textarea':
                            if (!empty($field->help) && trim($field->help) != '') {
                                $form->textarea($field->name, $field->title)
                                    ->help($field->help)
                                    ->placeholder($holder)
                                    ->value($field->value);
                            } else {
                                $form->textarea($field->name, $field->title)
                                    ->placeholder($holder)
                                    ->value($field->value);
                            }
                            break;
                        case 'editor':
                            if (!empty($field->help) && trim($field->help) != '') {
                                $form->editor($field->name, $field->title)
                                    ->help($field->help)
                                    ->placeholder($holder)
                                    ->value($field->value);
                            } else {
                                $form->editor($field->name, $field->title)
                                    ->placeholder($holder)
                                    ->value($field->value);
                            }
                        default:
                            # code...
                            break;
                    }
                }
            });
        }

    }
}
